﻿using B.S.GridManage.Api.Write.AearService;
using B.S.GridManage.Api.Write.Application.Command.GridTeam;
using B.S.GridManage.Api.Write.DTOs;
using B.S.GridManage.Api.Write.PhsicalGridService;
using B.S.GridManage.Api.Write.TeamService;
using B.S.GridManage.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.GridManage.Api.Write.Controllers
{
    /// <summary>
    /// 网格队伍控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TeamController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ServiceDiscovery teamService;
        private readonly AreaServiceDiscovery areaService;
        private readonly PhsicalGridDiscovery gridDiscovery;

        public TeamController(IMediator mediator,TeamService.ServiceDiscovery teamService,AreaServiceDiscovery areaService,PhsicalGridService.PhsicalGridDiscovery gridDiscovery)
        {
            this.mediator = mediator;
            this.teamService = teamService;
            this.areaService = areaService;
            this.gridDiscovery = gridDiscovery;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpPost]
        public   Task<ApiResult<int>> AddGridTeam(AddGridTeamCommand request) { 
            return mediator.Send(request);
        }

        [HttpGet]
        public async Task<ApiResult<List<GridTeamDto>>> GetTeamListFromReadService(string? areaName,string? gridName) {

            var url =await teamService.GetUrlAsync("B.S.GridManage.Api.Read");

            return await  teamService.GetDataFromReadAsync(url, $"/api/Team/GetGridTeamList?AreaName={areaName}&gridName={gridName}");
        }

        [HttpGet]
        public async Task<ApiResult<List<AreaTreeDto>>> GetAreaTree() {

            var url =await areaService.GetUrlAsync("B.S.GridManage.Api.Read");
            return await areaService.GetAreaTreeAsync(url+ "/api/Area/GetAreaTree");
        }



        ///// <summary>
        ///// 网格名称
        ///// </summary>
        //public string? GridName { get; set; }
        ///// <summary>
        ///// 所属区域名称
        ///// </summary>
        //public string? AreaName { get; set; }
        [HttpGet]
        public async Task<ApiResult<List<PhysicalGridDto>>> SearchPhysical(string? areaName, string? gridName) {
            var url =await gridDiscovery.GetUrlAsync("B.S.GridManage.Api.Read");
            return await gridDiscovery.SearchPhysicalAsync(url+ $"/api/PhsicalGrid/SearchPhysical?AreaName={areaName}&gridName={gridName}");
        }
    }
}
